
UNIT RotateKCHR;

INTERFACE

uses
MemTypes, QuickDraw, OSIntf, ToolIntf,PackIntf, Script;

PROCEDURE RotateKCHRs;

IMPLEMENTATION

PROCEDURE SetSICN; FORWARD;

PROCEDURE RotateKCHRs;
VAR
	keyresult: Longint;
	scriptsinstalled: Longint;
	newscript: Longint;
	theType: ResType;
	name: Str255;
	theID: integer;
	I: integer;
	err: OSerr;
	appscript: longint;
	totalKCHR: integer;
	KCHRHdl: Handle;

BEGIN
	SetSICN;
	scriptsinstalled := GetEnvirons(smEnabled);  {how many scripts?}
	
	IF scriptsinstalled > 1 THEN 	{more than roman}
		appscript := GetEnvirons(smKeyScript)   {so we need to get the current script}
	ELSE
		appscript := smRoman;
		
	{get all the current KCHR resource}
	keyresult := GetScript(appscript,smScriptKeys);
	
	{now cycle through the available KCHRs}
	totalKCHR := CountResources('KCHR');
	
	
	I := 1;
	REPEAT
		SetResLoad(FALSE);    {don't load the resources while we check}
		KCHRhdl := GetIndResource('KCHR',I);
		I := I + 1;
		GetResInfo(KCHRhdl,theID,theType,name);
	UNTIL (I > totalKCHR) OR ( theID = keyresult );
	
	
	IF ( I > totalKCHR ) THEN
		theID := 1
	ELSE
		theID := I;
		
	{we are ready to really load it}
	SetResLoad(TRUE);    {don't load the resources while we check}
	KCHRhdl := GetIndResource('KCHR',theID);
	GetResInfo(KCHRhdl,theID,theType,name);  {and get info on it}

	IF theID >= $4000 THEN	{it doesn't belong to Roman}
		newscript := ((theID - $4000) DIV 512) +1  {what script does it belong to?}
	ELSE
		newscript := smRoman;
		
	IF newscript <> appscript THEN BEGIN  {we have to switch scripts too}
		err := SetEnvirons(smKeyScript,newscript);
		appscript := newscript;
	END;
	
	{now change the KCHR}
	err := SetScript(appscript,smScriptKeys,theID);
	err := SetScript(appscript,smScriptIcon,theID);
	KeyScript(appscript);
		
END;

PROCEDURE SetSICN;
VAR
	SICNstate: Longint;
	err: Oserr;
	
BEGIN
	SICNstate := GetEnvirons(smGenFlags);
	BSET(SICNstate,smfShowIcon);
	
	err := SetEnvirons(smGenFlags,SICNstate);
	DrawMenuBar;
END;

END.
